const app = getApp();

Page({
  data: {
    showIndex: false,
    lastX: 0,
    showModalData: '',
    lastY: 0,
    level: '0',
    swIndex: '',
    showModal: false,
    current:0,
    isAuthorize:false
  },
  onLoad: function () {
    var that = this
    that.setData({
      padddingTop: wx.getSystemInfoSync().windowHeight
    })
  },
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    });
    var that = this;
  
    wx.getUserInfo({
      success: res => {
        app.globalData.userInfo = res.userInfo;
        that.setData({
          isAuthorize: true
        })
      },
      fail: res => {
        isAuthorize: false
      }
    });

    if (app.globalData.userData.openOId) {
      wx.request({
        url: app.globalData.phpUrl + 'Question/api',
        method: "POST",
        data: { api_name: 'table', openid: app.globalData.userData.openOId },
        success: function (data) {
          console.log('首页问卷信息', data.data);
          var list = data.data.data.list
          for (var nn = 0; nn < list.length; nn++) {
            list[nn].titleLength = that.strLen(list[nn].title)
            list[nn].describeLength = that.strLen(list[nn].describe)
          };
          that.setData({
            showIndex: true,
            list: list
          });
          wx.hideLoading();
        },
        fail: function (e) {
          console.log(e)
        }
      })
    }
    app.globalData.loadIndex = function (opId) {
      wx.request({
        url: app.globalData.phpUrl + 'Question/api',
        method: "POST",
        data: { api_name: 'table', openid: opId },
        success: function (data) {
          console.log('首页问卷信息', data.data);
          var list = data.data.data.list
          for (var nn = 0; nn < list.length; nn++) {
            list[nn].titleLength = that.strLen(list[nn].title)
            list[nn].describeLength = that.strLen(list[nn].describe)
          };
          that.setData({
            showIndex: true,
            list: list
          });
          wx.hideLoading();
        },
        fail: function (e) {
          console.log(e)
        }
      })
    };
  },
  
  strLen: function (str) {
    var len = 0;
    for (var i = 0; i < str.length; i++) {
      var c = str.charCodeAt(i);
      //单字节加1 
      if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
        len++;
      }
      else {
        len += 2;
      }
    }
    return len;
  },

  swiperChange:function(e){
    var that = this;
    that.setData({
      current: e.detail.current
    })

  },

  more: function (e) {
    this.setData({
      showModalData: e.target.dataset.showmodaldata,
      showModal: true
    });
  },
  hideModal: function () {
    this.setData({
      showModalData: '',
      showModal: false
    });
  },


  nvgto: function (event) {
    var that = this;
    wx.navigateTo({
      url: '../reply/reply?id=' + event.target.dataset.id + '&opid=' + app.globalData.userData.openOId + '&cover=' + that.data.list[event.target.dataset.index].cover + '&title=' + that.data.list[event.target.dataset.index].title + '&question_count=' + that.data.list[event.target.dataset.index].question_count + '&question_num=' + event.target.dataset.num + '&company=' + event.target.dataset.company
    });
        
  },

  noAnserwer: function (event) {
    var that = this;
    var code = event.target.dataset.va;
    if (code == 0){
      wx.showToast({
        title: '标签等级不够!',
        icon: 'none',
        duration: 2000,
      });
    }
    if (code == 2) {
      wx.showToast({
        title: '已回答过!',
        icon: 'none',
        duration: 2000,
      });
    }
    if (code == 3) {
      wx.showToast({
        title: '未关注公众号!',
        icon: 'none',
        duration: 2000,
      });
    }
  },

  authorize: function (event){
    var that = this;
    if (event.detail.rawData){
      wx.showLoading({
        title: '加载问卷中...',
      });
      app.globalData.userInfo = JSON.parse(event.detail.rawData);
      wx.request({
        url: app.globalData.phpUrl + 'index/getUnionId',
        method: "POST",
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        data: { openid: app.globalData.userData.openOId, encrypteddata: event.detail.encryptedData, iv: event.detail.iv },
        success: function (e) {
          wx.navigateTo({
            url: '../reply/reply?id=' + event.target.dataset.id + '&opid=' + app.globalData.userData.openOId + '&cover=' + that.data.list[event.target.dataset.index].cover + '&title=' + that.data.list[event.target.dataset.index].title + '&question_count=' + that.data.list[event.target.dataset.index].question_count + '&question_num=' + event.target.dataset.num + '&company=' + event.target.dataset.company
          });
          wx.hideLoading();
        }
      })
    }else{
      wx.showToast({
        title: '未获取你的信息,请接受授权获取您的信息!',
        icon: 'none',
        duration: 2000,
      });
    }
  }
})

